use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::mapper::ValueMapper;
use crate::modeling::{
    Class, ClassAttributeBuilder, ClassBuilder, ClassGetter, ClassRelationBuilder,
    RelationRelatedType, ValueType,
};
use crate::modeling::{
    ClassAttributePersistence, ClassPersistence, ClassPersistenceBuilder, ClassPersistenceGetter,
    ClassRelation, ClassRelationGetter, Value,
};

use super::{Model, ModelProvider, ModelType};

const MODEL_TYPE_SETTING_CLASS_IDENTIFIER: &'static str = "g01_model_type_setting";

const MODEL_TYPE_SETTING_CLASS_NAME: &'static str = "model_type_setting";

const MODEL_TYPE_SETTING_TABLE_NAME: &'static str = "_g01_meta_model_type_setting";

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "ModelTypeSettingBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct ModelTypeSetting {
    /// id
    pub id: i64,
    /// 类型
    pub model_type: ModelType,
    /// default_model
    pub default_provider: String,
    /// default_model
    pub default_model: String,
    /// 创建时间
    pub created_at: i64,
    /// 更新时间
    pub updated_at: i64,
}

impl ValueMapper for ModelTypeSetting {}
//
crate::impl_from_value!(ModelTypeSetting);
crate::impl_class_persistence_getter!(ModelTypeSetting, MODEL_TYPE_SETTING_TABLE_NAME);
crate::impl_class_relations_getter!(
    ModelTypeSetting,
    vec![
        ClassRelationBuilder::default()
            .src_identifier(Model::get_identifier()?)
            .target_identifier(Self::get_identifier()?)
            .related_type(RelationRelatedType::Keys)
            .related_at("default_model")
            .build()?,
        ClassRelationBuilder::default()
            .src_identifier(ModelProvider::get_identifier()?)
            .target_identifier(Self::get_identifier()?)
            .related_type(RelationRelatedType::Keys)
            .related_at("default_provider")
            .build()?
    ]
);

impl ClassGetter for ModelTypeSetting {
    fn get_name() -> anyhow::Result<String> {
        Ok(String::from(MODEL_TYPE_SETTING_CLASS_NAME))
    }
    fn get_identifier() -> anyhow::Result<String> {
        Ok(String::from(MODEL_TYPE_SETTING_CLASS_IDENTIFIER))
    }
    fn get_class() -> anyhow::Result<Class> {
        let mut attribute_builder = ClassAttributeBuilder::default();
        attribute_builder.class_identifier(MODEL_TYPE_SETTING_CLASS_IDENTIFIER);

        let mut commons = crate::modeling::simple_attributes_with_builder(&attribute_builder)?;
        let mut append = vec![
            attribute_builder
                .clone()
                .name("model_type")
                .title("类型")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("default_provider")
                .title("模型供应商")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("default_model")
                .title("模型")
                .data_type(ValueType::String)
                .build()?,
        ];
        append.append(&mut commons);
        append.sort_by(|a, b| a.position.cmp(&b.position));

        let clazz = ClassBuilder::default()
            .identifier(MODEL_TYPE_SETTING_CLASS_IDENTIFIER)
            .name(MODEL_TYPE_SETTING_CLASS_NAME)
            .data_is_type(false)
            .is_persistent(true)
            .primary_key(String::from("identifier"))
            .attributes(append)
            .build()?;
        Ok(clazz)
    }
}
